﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace WF4Host
{
    public class MainViewPresenter : Grid
    {
        public HostingHelper HostingHelper
        {
            get { return (HostingHelper)GetValue(HostingHelperProperty); }
            set { SetValue(HostingHelperProperty, value); }
        }

        public static readonly DependencyProperty HostingHelperProperty =
            DependencyProperty.Register("HostingHelper", typeof(HostingHelper)
            , typeof(MainViewPresenter)
            , new UIPropertyMetadata(null, new PropertyChangedCallback(OnHostingHelperChanged)));

        protected static void OnHostingHelperChanged(DependencyObject dobj
            , DependencyPropertyChangedEventArgs dpcea)
        {
            if (dobj is MainViewPresenter)
            {
                var mvp = dobj as MainViewPresenter;
                mvp.Children.Clear();
                if (dpcea.NewValue != null)
                {
                    var host = dpcea.NewValue as HostingHelper;
                    host.Designer.View.SetValue(Panel.HorizontalAlignmentProperty, HorizontalAlignment.Stretch);
                    host.Designer.View.SetValue(Panel.VerticalAlignmentProperty, VerticalAlignment.Stretch);
                    mvp.Children.Add(host.Designer.View);
                }
            }
        }
    }
}
